package com.wkhyapp.lm.view;

import android.graphics.Color;
import android.os.Build;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.loadmore.LoadMoreView;
import com.wkhyapp.lm.R;
import com.wkhyapp.lm.adapter.GoodsBatchListAdapter;
import com.wkhyapp.lm.adapter.GoodsXCListAdapter;
import com.wkhyapp.lm.base.SuperActivity;
import com.wkhyapp.lm.contract.XiangCePresenter;
import com.wkhyapp.lm.contract.XiangCeView;
import com.wkhyapp.lm.http.net.AddGoodsRequest;
import com.wkhyapp.lm.http.vo.Goods;
import com.wkhyapp.lm.utils.MemberUtils;
import com.wkhyapp.lm.utils.StatusBarFontUtil;
import com.wkhyapp.lm.weigit.EmptyView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

public class BatchActivity  extends SuperActivity<XiangCePresenter> implements XiangCeView {


    @BindView(R.id.refresh_view)
    SwipeRefreshLayout refresh_view;
    @BindView(R.id.recycler_view)
    RecyclerView recycler_view;
    private GoodsBatchListAdapter goodsBatchListAdapter;
    private List<Goods> goodses;
    private List<Goods> mCheckedData = new ArrayList<>();//将选中
    private int mCurrentPage = 1;
    @BindView(R.id.back_iv)
    ImageView back_iv;
    @BindView(R.id.plgl_tv)
    TextView plgl_tv;
    private View empty_view;

    @BindView(R.id.qx_rb)
    CheckBox qx_rb;
    @Override
    public void initUI() {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            translucentStatusBar();
        }
        StatusBarFontUtil.setLightStatusBarColor(this);

        goodses = new ArrayList<>();
        empty_view = EmptyView.getView(mContext,R.layout.empty_view,R.drawable.wuzhuangtai,"暂无商品");
        goodsBatchListAdapter = new GoodsBatchListAdapter(R.layout.item_batch_goods,goodses);
        refresh_view.setColorSchemeColors(Color.parseColor("#FFEC48"),Color.parseColor("#FFEC48"));
        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recycler_view.setLayoutManager(manager);
        recycler_view.setAdapter(goodsBatchListAdapter);
        refresh_view.setRefreshing(false);
        mPresenter.getData("");


}


    @Override
    protected void initListener() {

        back_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        goodsBatchListAdapter.setLoadMoreView(new LoadMoreView() {
            @Override
            public int getLayoutId() {
                return R.layout.view_index_load_more;
            }

            @Override
            protected int getLoadingViewId() {
                return com.chad.library.R.id.load_more_loading_view;
            }
            @Override
            protected int getLoadFailViewId() {
                return com.chad.library.R.id.load_more_load_fail_view;
            }
            @Override
            protected int getLoadEndViewId() {
                return com.chad.library.R.id.load_more_load_end_view;
            }
        });

        goodsBatchListAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                mCurrentPage = mCurrentPage+1;
                mPresenter.getDataMore(mCurrentPage,"");

            }
        });
        refresh_view.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mCurrentPage = 1;
                mPresenter.getData("");
            }
        });


        plgl_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<Long> ids = new ArrayList<Long>();
                for (Map.Entry<Integer,Boolean> entry : goodsBatchListAdapter.getMap().entrySet()) {
                    if(entry.getValue()){
                        ids.add(goodses.get(entry.getKey()).getId());
                    }
                }

                if(ids.size()>0){
                    showLoad("正在刷新...");
                    mPresenter.refreshGoods(ids);
                }else {
                    TToast("请现选择商品");
                }
            }
        });

        qx_rb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    goodsBatchListAdapter.initCheck(true);
                }else{
                    goodsBatchListAdapter.initCheck(false);
                }
                goodsBatchListAdapter.notifyDataSetChanged();
            }
        });



    }

    @Override
    protected void afterCreate() {

    }


    @Override
    public void setGoods(List<Goods> list) {
        goodses.clear();
        if(list!=null&&list.size()>0){
            goodses.addAll(list);
            goodsBatchListAdapter.setData(goodses);
            goodsBatchListAdapter.removeFooterView(empty_view);
        }else{
            goodsBatchListAdapter.removeFooterView(empty_view);
            goodsBatchListAdapter.addFooterView(empty_view);
        }
        goodsBatchListAdapter.notifyDataSetChanged();
        refresh_view.setRefreshing(false);
        goodsBatchListAdapter.loadMoreComplete();

    }

    @Override
    public void setGoodsMore(List<Goods> list) {
        if(list.size()==0){
            goodsBatchListAdapter.loadMoreComplete();
            goodsBatchListAdapter.loadMoreEnd();
        }else{
            goodses.addAll(list);
            goodsBatchListAdapter.notifyDataSetChanged();
            goodsBatchListAdapter.loadMoreComplete();
        }
    }

    @Override
    public void setSucc(String str) {
        TToast(str);
        mPresenter.getData("");
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_batch;
    }

    @Override
    protected XiangCePresenter createPresenter() {
        return mPresenter = new XiangCePresenter(this);
    }




}
